package com.xxd.spring.boot.demo.lihonghai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xxd.spring.boot.demo.lihonghai.Eeception.MyException;
import com.xxd.spring.boot.demo.lihonghai.entity.User;
import com.xxd.spring.boot.demo.lihonghai.mapper.UserMapper;
import com.xxd.spring.boot.demo.lihonghai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
@Autowired
  private UserMapper userMapper;
    @Override
    public int regsit(User user) throws MyException {
        QueryWrapper queryWrapper= new QueryWrapper();
        queryWrapper.eq("username",user.getUsername());
        User dbUser=userMapper.selectOne(queryWrapper);
          if(dbUser != null)
          {
              throw new MyException("用户已存在","123");
          }
          return userMapper.insert(user);
    }

    @Override
    public User login (String username, String password) throws MyException
    {
        User user = userMapper.selectByUsernameAndPassword(username,password);
        if(user == null)
        {
            throw new MyException("用户名或密码错误","115");
        }
        return user;
    }
}
